package cn.edu.ujn.booksshop.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.Book;
import cn.edu.ujn.booksshop.service.IBookService;
import cn.edu.ujn.util.Page;

@Controller
public class BookController {

	@Autowired
	private IBookService bookService;

	@Autowired
	private Book bookDao;

	@RequestMapping(value = "/book/list.action")
	public String list(Model model, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "20") Integer rows) {
		Page<Book> pages = this.bookService.selectAll(page, rows);
		model.addAttribute("page", pages);
		return "bookManagement";
	}

	@RequestMapping(value = "book/delete.action")
	@ResponseBody
	public String deleteByPrimaryKey(Integer id) {
		int deleteByPrimaryKey = this.bookService.deleteByPrimaryKey(id);
		if (deleteByPrimaryKey > 0) {
			return "OK";
		} else {
			return "FAIL";
		}

	}

	@RequestMapping(value = "book/create.action")
	@ResponseBody
	public String add(Book book) {
		int insert = this.bookService.insertSelective(book);
		if (insert > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	@RequestMapping("book/update.action")
	@ResponseBody
	public String update(Book book) {
		int updateByPrimaryKeySelective = this.bookService.updateByPrimaryKeySelective(book);
		if (updateByPrimaryKeySelective > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	@RequestMapping("book/getBookById.action")
	@ResponseBody
	public Book getBookById(Integer id) {
		Book key = this.bookService.selectByPrimaryKey(id);
		return key;
	}

}
